home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / GCC-2.3.3r12 / Sources / regclass.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-11  |  48.4 KB  |  1,698 lines  |  [TEXT/MPS ]

  1. /* Compute register class preferences for pseudo-registers.
  2.    Copyright (C) 1987, 1988, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* This should be removed from here. */
  21. #ifdef TARGET_GLOBALIZE_CODE
  22. char *reg_names_no_globalize[] = REGISTER_NAMES_NO_GLOBALIZE;
  23. #endif
  24.  
  25. /* This file contains two passes of the compiler: reg_scan and reg_class.
  26.    It also defines some tables of information about the hardware registers
  27.    and a function init_reg_sets to initialize the tables.  */
  28.  
  29. #include "config.h"
  30. #include "rtl.h"
  31. #include "hard-reg-set.h"
  32. #include "flags.h"
  33. #include "basic-block.h"
  34. #include "regs.h"
  35. #include "insn-config.h"
  36. #include "recog.h"
  37. #include "reload.h"
  38. #include "real.h"
  39.  
  40. #ifdef PTR_HACK
  41. #ifdef TARGET_GLOBALIZE_CODE
  42. char *reg_names_no_globalize[] = REGISTER_NAMES_NO_GLOBALIZE;
  43. #endif
  44. #endif /* PTR_HACK */
  45.  
  46. #ifndef REGISTER_MOVE_COST
  47. #define REGISTER_MOVE_COST(x, y) 2
  48. #endif
  49.  
  50. #ifndef MEMORY_MOVE_COST
  51. #define MEMORY_MOVE_COST(x) 4
  52. #endif
  53.  
  54. /* If we have auto-increment or auto-decrement and we can have secondary
  55.    reloads, we are not allowed to use classes requiring secondary
  56.    reloads for psuedos auto-incremented since reload can't handle it.  */
  57.  
  58. #ifdef AUTO_INC_DEC
  59. #if defined(SECONDARY_INPUT_RELOAD_CLASS) || defined(SECONDARY_OUTPUT_RELOAD_CLASS)
  60. #define FORBIDDEN_INC_DEC_CLASSES
  61. #endif
  62. #endif
  63.  
  64. /* Register tables used by many passes.  */
  65.  
  66. /* Indexed by hard register number, contains 1 for registers
  67.    that are fixed use (stack pointer, pc, frame pointer, etc.).
  68.    These are the registers that cannot be used to allocate
  69.    a pseudo reg whose life does not cross calls.  */
  70.  
  71. char fixed_regs[FIRST_PSEUDO_REGISTER];
  72.  
  73. /* Same info as a HARD_REG_SET.  */
  74.  
  75. HARD_REG_SET fixed_reg_set;
  76.  
  77. /* Data for initializing the above.  */
  78.  
  79. static char initial_fixed_regs[] = FIXED_REGISTERS;
  80.  
  81. /* Indexed by hard register number, contains 1 for registers
  82.    that are fixed use or are clobbered by function calls.
  83.    These are the registers that cannot be used to allocate
  84.    a pseudo reg whose life crosses calls.  */
  85.  
  86. char call_used_regs[FIRST_PSEUDO_REGISTER];
  87.  
  88. /* Same info as a HARD_REG_SET.  */
  89.  
  90. HARD_REG_SET call_used_reg_set;
  91.  
  92. /* Data for initializing the above.  */
  93.  
  94. static char initial_call_used_regs[] = CALL_USED_REGISTERS;
  95.   
  96. /* Indexed by hard register number, contains 1 for registers that are
  97.    fixed use -- i.e. in fixed_regs -- or a function value return register
  98.    or STRUCT_VALUE_REGNUM or STATIC_CHAIN_REGNUM.  These are the
  99.    registers that cannot hold quantities across calls even if we are
  100.    willing to save and restore them.  */
  101.  
  102. char call_fixed_regs[FIRST_PSEUDO_REGISTER];
  103.  
  104. /* The same info as a HARD_REG_SET.  */
  105.  
  106. HARD_REG_SET call_fixed_reg_set;
  107.  
  108. /* Number of non-fixed registers.  */
  109.  
  110. int n_non_fixed_regs;
  111.  
  112. /* Indexed by hard register number, contains 1 for registers
  113.    that are being used for global register decls.
  114.    These must be exempt from ordinary flow analysis
  115.    and are also considered fixed.  */
  116.  
  117. char global_regs[FIRST_PSEUDO_REGISTER];
  118.   
  119. /* Table of register numbers in the order in which to try to use them.  */
  120. #ifdef REG_ALLOC_ORDER
  121. int reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
  122. #endif
  123.  
  124. /* For each reg class, a HARD_REG_SET saying which registers are in it.  */
  125.  
  126. HARD_REG_SET reg_class_contents[N_REG_CLASSES];
  127.  
  128. /* The same information, but as an array of ints.  We copy from these
  129.    ints to the table above.  This is done so that the tm.h files do
  130.    not have to be aware of the wordsize for machines with <= 64 regs.  */
  131.  
  132. #define N_REG_INTS  \
  133.   ((FIRST_PSEUDO_REGISTER + (HOST_BITS_PER_INT - 1)) / HOST_BITS_PER_INT)
  134.  
  135. static int int_reg_class_contents[N_REG_CLASSES][N_REG_INTS] 
  136.   = REG_CLASS_CONTENTS;
  137.  
  138. /* For each reg class, number of regs it contains.  */
  139.  
  140. int reg_class_size[N_REG_CLASSES];
  141.  
  142. /* For each reg class, table listing all the containing classes.  */
  143.  
  144. enum reg_class reg_class_superclasses[N_REG_CLASSES][N_REG_CLASSES];
  145.  
  146. /* For each reg class, table listing all the classes contained in it.  */
  147.  
  148. enum reg_class reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
  149.  
  150. /* For each pair of reg classes,
  151.    a largest reg class contained in their union.  */
  152.  
  153. enum reg_class reg_class_subunion[N_REG_CLASSES][N_REG_CLASSES];
  154.  
  155. /* For each pair of reg classes,
  156.    the smallest reg class containing their union.  */
  157.  
  158. enum reg_class reg_class_superunion[N_REG_CLASSES][N_REG_CLASSES];
  159.  
  160. /* Array containing all of the register names */
  161.  
  162. char *reg_names[] = REGISTER_NAMES;
  163.  
  164. /* Indexed by n, gives number of times (REG n) is set or clobbered.
  165.    This information remains valid for the rest of the compilation
  166.    of the current function; it is used to control register allocation.
  167.  
  168.    This information applies to both hard registers and pseudo registers,
  169.    unlike much of the information above.  */
  170.  
  171. short *reg_n_sets;
  172.  
  173. /* Maximum cost of moving from a register in one class to a register in
  174.    another class.  Based on REGISTER_MOVE_COST.  */
  175.  
  176. static int move_cost[N_REG_CLASSES][N_REG_CLASSES];
  177.  
  178. /* Similar, but here we don't have to move if the first index is a subset
  179.    of the second so in that case the cost is zero.  */
  180.  
  181. static int may_move_cost[N_REG_CLASSES][N_REG_CLASSES];
  182.  
  183. #ifdef FORBIDDEN_INC_DEC_CLASSES
  184.  
  185. /* These are the classes that regs which are auto-incremented or decremented
  186.    cannot be put in.  */
  187.  
  188. static int forbidden_inc_dec_class[N_REG_CLASSES];
  189.  
  190. /* Indexed by n, is non-zero if (REG n) is used in an auto-inc or auto-dec
  191.    context.  */
  192.  
  193. static char *in_inc_dec;
  194.  
  195. #endif /* FORBIDDEN_INC_DEC_CLASSES */
  196.  
  197. /* Function called only once to initialize the above data on reg usage.
  198.    Once this is done, various switches may override.  */
  199.  
  200. void
  201. init_reg_sets ()
  202. {
  203.   register int i, j;
  204.  
  205.   /* First copy the register information from the initial int form into
  206.      the regsets.  */
  207.  
  208.   for (i = 0; i < N_REG_CLASSES; i++)
  209.     {
  210.       CLEAR_HARD_REG_SET (reg_class_contents[i]);
  211.  
  212.       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
  213.     if (int_reg_class_contents[i][j / HOST_BITS_PER_INT]
  214.         & (1 << (j % HOST_BITS_PER_INT)))
  215.       SET_HARD_REG_BIT (reg_class_contents[i], j);
  216.     }
  217.  
  218.   bcopy (initial_fixed_regs, fixed_regs, sizeof fixed_regs);
  219.   bcopy (initial_call_used_regs, call_used_regs, sizeof call_used_regs);
  220.   bzero (global_regs, sizeof global_regs);
  221.  
  222.   /* Compute number of hard regs in each class.  */
  223.  
  224.   bzero (reg_class_size, sizeof reg_class_size);
  225.   for (i = 0; i < N_REG_CLASSES; i++)
  226.     for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
  227.       if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
  228.     reg_class_size[i]++;
  229.  
  230.   /* Initialize the table of subunions.
  231.      reg_class_subunion[I][J] gets the largest-numbered reg-class
  232.      that is contained in the union of classes I and J.  */
  233.  
  234.   for (i = 0; i < N_REG_CLASSES; i++)
  235.     {
  236.       for (j = 0; j < N_REG_CLASSES; j++)
  237.     {
  238. #ifdef HARD_REG_SET
  239.       register        /* Declare it register if it's a scalar.  */
  240. #endif
  241.         HARD_REG_SET c;
  242.       register int k;
  243.  
  244.       COPY_HARD_REG_SET (c, reg_class_contents[i]);
  245.       IOR_HARD_REG_SET (c, reg_class_contents[j]);
  246.       for (k = 0; k < N_REG_CLASSES; k++)
  247.         {
  248.           GO_IF_HARD_REG_SUBSET (reg_class_contents[k], c,
  249.                      subclass1);
  250.           continue;
  251.  
  252.         subclass1:
  253.           /* keep the largest subclass */        /* SPEE 900308 */
  254.           GO_IF_HARD_REG_SUBSET (reg_class_contents[k],
  255.                      reg_class_contents[(int) reg_class_subunion[i][j]],
  256.                      subclass2);
  257.           reg_class_subunion[i][j] = (enum reg_class) k;
  258.         subclass2:
  259.           ;
  260.         }
  261.     }
  262.     }
  263.  
  264.   /* Initialize the table of superunions.
  265.      reg_class_superunion[I][J] gets the smallest-numbered reg-class
  266.      containing the union of classes I and J.  */
  267.  
  268.   for (i = 0; i < N_REG_CLASSES; i++)
  269.     {
  270.       for (j = 0; j < N_REG_CLASSES; j++)
  271.     {
  272. #ifdef HARD_REG_SET
  273.       register        /* Declare it register if it's a scalar.  */
  274. #endif
  275.         HARD_REG_SET c;
  276.       register int k;
  277.  
  278.       COPY_HARD_REG_SET (c, reg_class_contents[i]);
  279.       IOR_HARD_REG_SET (c, reg_class_contents[j]);
  280.       for (k = 0; k < N_REG_CLASSES; k++)
  281.         GO_IF_HARD_REG_SUBSET (c, reg_class_contents[k], superclass);
  282.  
  283.     superclass:
  284.       reg_class_superunion[i][j] = (enum reg_class) k;
  285.     }
  286.     }
  287.  
  288.   /* Initialize the tables of subclasses and superclasses of each reg class.
  289.      First clear the whole table, then add the elements as they are found.  */
  290.  
  291.   for (i = 0; i < N_REG_CLASSES; i++)
  292.     {
  293.       for (j = 0; j < N_REG_CLASSES; j++)
  294.     {
  295.       reg_class_superclasses[i][j] = LIM_REG_CLASSES;
  296.       reg_class_subclasses[i][j] = LIM_REG_CLASSES;
  297.     }
  298.     }
  299.  
  300.   for (i = 0; i < N_REG_CLASSES; i++)
  301.     {
  302.       if (i == (int) NO_REGS)
  303.     continue;
  304.  
  305.       for (j = i + 1; j < N_REG_CLASSES; j++)
  306.     {
  307.       enum reg_class *p;
  308.  
  309.       GO_IF_HARD_REG_SUBSET (reg_class_contents[i], reg_class_contents[j],
  310.                  subclass);
  311.       continue;
  312.     subclass:
  313.       /* Reg class I is a subclass of J.
  314.          Add J to the table of superclasses of I.  */
  315.       p = ®_class_superclasses[i][0];
  316.       while (*p != LIM_REG_CLASSES) p++;
  317.       *p = (enum reg_class) j;
  318.       /* Add I to the table of superclasses of J.  */
  319.       p = ®_class_subclasses[j][0];
  320.       while (*p != LIM_REG_CLASSES) p++;
  321.       *p = (enum reg_class) i;
  322.     }
  323.     }
  324.  
  325.   /* Initialize the move cost table.  Find every subset of each class
  326.      and take the maximum cost of moving any subset to any other.  */
  327.  
  328.   for (i = 0; i < N_REG_CLASSES; i++)
  329.     for (j = 0; j < N_REG_CLASSES; j++)
  330.       {
  331.     int cost = i == j ? 2 : REGISTER_MOVE_COST (i, j);
  332.     enum reg_class *p1, *p2;
  333.  
  334.     for (p2 = ®_class_subclasses[j][0]; *p2 != LIM_REG_CLASSES; p2++)
  335.       if (*p2 != i)
  336.         cost = MAX (cost, REGISTER_MOVE_COST (i, *p2));
  337.  
  338.     for (p1 = ®_class_subclasses[i][0]; *p1 != LIM_REG_CLASSES; p1++)
  339.       {
  340.         if (*p1 != j)
  341.           cost = MAX (cost, REGISTER_MOVE_COST (*p1, j));
  342.  
  343.         for (p2 = ®_class_subclasses[j][0];
  344.          *p2 != LIM_REG_CLASSES; p2++)
  345.           if (*p1 != *p2)
  346.         cost = MAX (cost, REGISTER_MOVE_COST (*p1, *p2));
  347.       }
  348.  
  349.     move_cost[i][j] = cost;
  350.  
  351.     if (reg_class_subset_p (i, j))
  352.       cost = 0;
  353.  
  354.     may_move_cost[i][j] = cost;
  355.       }
  356. }
  357.  
  358. /* After switches have been processed, which perhaps alter
  359.    `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs.  */
  360.  
  361. void
  362. init_reg_sets_1 ()
  363. {
  364.   register int i;
  365.  
  366.   /* This macro allows the fixed or call-used registers
  367.      to depend on target flags.  */
  368.  
  369. #ifdef CONDITIONAL_REGISTER_USAGE
  370.   CONDITIONAL_REGISTER_USAGE;
  371. #endif
  372.  
  373.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  374.     if (global_regs[i])
  375.       {
  376.     if (call_used_regs[i] && ! fixed_regs[i])
  377.       warning ("call-clobbered register used for global register variable");
  378.     fixed_regs[i] = 1;
  379.     /* Prevent saving/restoring of this reg.  */
  380.     call_used_regs[i] = 1;
  381.       }
  382.  
  383.   /* Initialize "constant" tables.  */
  384.  
  385.   CLEAR_HARD_REG_SET (fixed_reg_set);
  386.   CLEAR_HARD_REG_SET (call_used_reg_set);
  387.   CLEAR_HARD_REG_SET (call_fixed_reg_set);
  388.  
  389.   bcopy (fixed_regs, call_fixed_regs, sizeof call_fixed_regs);
  390. #ifdef STRUCT_VALUE_REGNUM
  391.   call_fixed_regs[STRUCT_VALUE_REGNUM] = 1;
  392. #endif
  393. #ifdef STATIC_CHAIN_REGNUM
  394.   call_fixed_regs[STATIC_CHAIN_REGNUM] = 1;
  395. #endif
  396.  
  397.   n_non_fixed_regs = 0;
  398.  
  399.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  400.     {
  401.       if (FUNCTION_VALUE_REGNO_P (i))
  402.     call_fixed_regs[i] = 1;
  403.       if (fixed_regs[i])
  404.     SET_HARD_REG_BIT (fixed_reg_set, i);
  405.       else
  406.     n_non_fixed_regs++;
  407.  
  408.       if (call_used_regs[i])
  409.     SET_HARD_REG_BIT (call_used_reg_set, i);
  410.       if (call_fixed_regs[i])
  411.     SET_HARD_REG_BIT (call_fixed_reg_set, i);
  412.     }
  413. }
  414.  
  415. /* Specify the usage characteristics of the register named NAME.
  416.    It should be a fixed register if FIXED and a
  417.    call-used register if CALL_USED.  */
  418.  
  419. void
  420. fix_register (name, fixed, call_used)
  421.      char *name;
  422.      int fixed, call_used;
  423. {
  424.   int i;
  425.  
  426.   /* Decode the name and update the primary form of
  427.      the register info.  */
  428.  
  429.   if ((i = decode_reg_name (name)) >= 0)
  430.     {
  431.       fixed_regs[i] = fixed;
  432.       call_used_regs[i] = call_used;
  433.     }
  434.   else
  435.     {
  436.       warning ("unknown register name: %s", name);
  437.     }
  438. }
  439.  
  440. /* Now the data and code for the `regclass' pass, which happens
  441.    just before local-alloc.  */
  442.  
  443. /* The `costs' struct records the cost of using a hard register of each class
  444.    and of using memory for each pseudo.  We use this data to set up
  445.    register class preferences.  */
  446.  
  447. struct costs
  448. {
  449.   int cost[N_REG_CLASSES];
  450.   int mem_cost;
  451. };
  452.  
  453. /* Record the cost of each class for each pseudo.  */
  454.  
  455. static struct costs *costs;
  456.  
  457. /* Record the same data by operand number, accumulated for each alternative
  458.    in an insn.  The contribution to a pseudo is that of the minimum-cost
  459.    alternative.  */
  460.  
  461. static struct costs op_costs[MAX_RECOG_OPERANDS];
  462.  
  463. /* (enum reg_class) prefclass[R] is the preferred class for pseudo number R.
  464.    This is available after `regclass' is run.  */
  465.  
  466. static char *prefclass;
  467.  
  468. /* altclass[R] is a register class that we should use for allocating
  469.    pseudo number R if no register in the preferred class is available.
  470.    If no register in this class is available, memory is preferred.
  471.  
  472.    It might appear to be more general to have a bitmask of classes here,
  473.    but since it is recommended that there be a class corresponding to the
  474.    union of most major pair of classes, that generality is not required. 
  475.  
  476.    This is available after `regclass' is run.  */
  477.  
  478. static char *altclass;
  479.  
  480. /* Record the depth of loops that we are in.  */
  481.  
  482. static int loop_depth;
  483.  
  484. /* Account for the fact that insns within a loop are executed very commonly,
  485.    but don't keep doing this as loops go too deep.  */
  486.  
  487. static int loop_cost;
  488.  
  489. static int copy_cost ();
  490. static void record_reg_classes ();
  491. static void record_address_regs ();
  492.  
  493.  
  494. /* Return the reg_class in which pseudo reg number REGNO is best allocated.
  495.    This function is sometimes called before the info has been computed.
  496.    When that happens, just return GENERAL_REGS, which is innocuous.  */
  497.  
  498. enum reg_class
  499. reg_preferred_class (regno)
  500.      int regno;
  501. {
  502.   if (prefclass == 0)
  503.     return GENERAL_REGS;
  504.   return (enum reg_class) prefclass[regno];
  505. }
  506.  
  507. enum reg_class
  508. reg_alternate_class (regno)
  509. {
  510.   if (prefclass == 0)
  511.     return ALL_REGS;
  512.  
  513.   return (enum reg_class) altclass[regno];
  514. }
  515.  
  516. /* This prevents dump_flow_info from losing if called
  517.    before regclass is run.  */
  518.  
  519. void
  520. regclass_init ()
  521. {
  522.   prefclass = 0;
  523. }
  524.  
  525. /* This is a pass of the compiler that scans all instructions
  526.    and calculates the preferred class for each pseudo-register.
  527.    This information can be accessed later by calling `reg_preferred_class'.
  528.    This pass comes just before local register allocation.  */
  529.  
  530. void
  531. regclass (f, nregs)
  532.      rtx f;
  533.      int nregs;
  534. {
  535. #ifdef REGISTER_CONSTRAINTS
  536.   register rtx insn;
  537.   register int i, j;
  538.   struct costs init_cost;
  539.   rtx set;
  540.   int pass;
  541.  
  542.   init_recog ();
  543.  
  544.   costs = (struct costs *) alloca (nregs * sizeof (struct costs));
  545.  
  546. #ifdef FORBIDDEN_INC_DEC_CLASSES
  547.  
  548.   in_inc_dec = (char *) alloca (nregs);
  549.  
  550.   /* Initialize information about which register classes can be used for
  551.      pseudos that are auto-incremented or auto-decremented.  It would
  552.      seem better to put this in init_reg_sets, but we need to be able
  553.      to allocate rtx, which we can't do that early.  */
  554.  
  555.   for (i = 0; i < N_REG_CLASSES; i++)
  556.     {
  557.       rtx r = gen_rtx (REG, VOIDmode, 0);
  558.       enum machine_mode m;
  559.  
  560.       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
  561.     if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
  562.       {
  563.         REGNO (r) = j;
  564.  
  565.         for (m = VOIDmode; (int) m < (int) MAX_MACHINE_MODE;
  566.          m = (enum machine_mode) ((int) m) + 1)
  567.           if (HARD_REGNO_MODE_OK (j, m))
  568.         {
  569.           PUT_MODE (r, m);
  570.           if (0
  571. #ifdef SECONDARY_INPUT_RELOAD_CLASS
  572.               || (SECONDARY_INPUT_RELOAD_CLASS (BASE_REG_CLASS, m, r)
  573.               != NO_REGS)
  574. #endif
  575. #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
  576.               || (SECONDARY_OUTPUT_RELOAD_CLASS (BASE_REG_CLASS, m, r)
  577.               != NO_REGS)
  578. #endif
  579.               )
  580.             forbidden_inc_dec_class[i] = 1;
  581.         }
  582.       }
  583.     }
  584. #endif /* FORBIDDEN_INC_DEC_CLASSES */
  585.  
  586.   init_cost.mem_cost = 10000;
  587.   for (i = 0; i < N_REG_CLASSES; i++)
  588.     init_cost.cost[i] = 10000;
  589.  
  590.   /* Normally we scan the insns once and determine the best class to use for
  591.      each register.  However, if -fexpensive_optimizations are on, we do so
  592.      twice, the second time using the tentative best classes to guide the
  593.      selection.  */
  594.  
  595.   for (pass = 0; pass <= flag_expensive_optimizations; pass++)
  596.     {
  597.       /* Zero out our accumulation of the cost of each class for each reg.  */
  598.  
  599.       bzero (costs, nregs * sizeof (struct costs));
  600.  
  601. #ifdef FORBIDDEN_INC_DEC_CLASSES
  602.       bzero (in_inc_dec, nregs);
  603. #endif
  604.  
  605.       loop_depth = 0, loop_cost = 1;
  606.  
  607.       /* Scan the instructions and record each time it would
  608.      save code to put a certain register in a certain class.  */
  609.  
  610.       for (insn = f; insn; insn = NEXT_INSN (insn))
  611.     {
  612.       char *constraints[MAX_RECOG_OPERANDS];
  613.       enum machine_mode modes[MAX_RECOG_OPERANDS];
  614.       int nalternatives;
  615.       int noperands;
  616.  
  617.       /* Show that an insn inside a loop is likely to be executed three
  618.          times more than insns outside a loop.  This is much more agressive
  619.          than the assumptions made elsewhere and is being tried as an
  620.          experiment.  */
  621.  
  622.       if (GET_CODE (insn) == NOTE
  623.           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
  624.         loop_depth++, loop_cost = 1 << (2 * MIN (loop_depth, 5));
  625.       else if (GET_CODE (insn) == NOTE
  626.            && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
  627.         loop_depth--, loop_cost = 1 << (2 * MIN (loop_depth, 5));
  628.  
  629.       else if ((GET_CODE (insn) == INSN
  630.             && GET_CODE (PATTERN (insn)) != USE
  631.             && GET_CODE (PATTERN (insn)) != CLOBBER
  632.             && GET_CODE (PATTERN (insn)) != ASM_INPUT)
  633.            || (GET_CODE (insn) == JUMP_INSN
  634.                && GET_CODE (PATTERN (insn)) != ADDR_VEC
  635.                && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
  636.            || GET_CODE (insn) == CALL_INSN)
  637.         {
  638.           if (GET_CODE (insn) == INSN
  639.           && (noperands = asm_noperands (PATTERN (insn))) >= 0)
  640.         {
  641.           decode_asm_operands (PATTERN (insn), recog_operand, NULL_PTR,
  642.                        constraints, modes);
  643.           nalternatives = (noperands == 0 ? 0
  644.                    : n_occurrences (',', constraints[0]) + 1);
  645.         }
  646.           else
  647.         {
  648.           int insn_code_number = recog_memoized (insn);
  649.           rtx note;
  650.  
  651.           set = single_set (insn);
  652.           insn_extract (insn);
  653.  
  654.           nalternatives = insn_n_alternatives[insn_code_number];
  655.           noperands = insn_n_operands[insn_code_number];
  656.  
  657.           /* If this insn loads a parameter from its stack slot, then
  658.              it represents a savings, rather than a cost, if the
  659.              parameter is stored in memory.  Record this fact.  */
  660.  
  661.           if (set != 0 && GET_CODE (SET_DEST (set)) == REG
  662.               && GET_CODE (SET_SRC (set)) == MEM
  663.               && (note = find_reg_note (insn, REG_EQUIV,
  664.                         NULL_RTX)) != 0
  665.               && GET_CODE (XEXP (note, 0)) == MEM)
  666.             {
  667.               costs[REGNO (SET_DEST (set))].mem_cost
  668.             -= (MEMORY_MOVE_COST (GET_MODE (SET_DEST (set)))
  669.                 * loop_cost);
  670.               record_address_regs (XEXP (SET_SRC (set), 0),
  671.                        BASE_REG_CLASS, loop_cost * 2);
  672.               continue;
  673.             }
  674.           
  675.           /* Improve handling of two-address insns such as
  676.              (set X (ashift CONST Y)) where CONST must be made to
  677.              match X. Change it into two insns: (set X CONST)
  678.              (set X (ashift X Y)).  If we left this for reloading, it
  679.              would probably get three insns because X and Y might go
  680.              in the same place. This prevents X and Y from receiving
  681.              the same hard reg.
  682.  
  683.              We can only do this if the modes of operands 0 and 1
  684.              (which might not be the same) are tieable and we only need
  685.              do this during our first pass.  */
  686.  
  687.           if (pass == 0 && optimize
  688.               && noperands >= 3
  689.               && insn_operand_constraint[insn_code_number][1][0] == '0'
  690.               && insn_operand_constraint[insn_code_number][1][1] == 0
  691.               && CONSTANT_P (recog_operand[1])
  692.               && ! rtx_equal_p (recog_operand[0], recog_operand[1])
  693.               && ! rtx_equal_p (recog_operand[0], recog_operand[2])
  694.               && GET_CODE (recog_operand[0]) == REG
  695.               && MODES_TIEABLE_P (GET_MODE (recog_operand[0]),
  696.                       insn_operand_mode[insn_code_number][1]))
  697.             {
  698.               rtx previnsn = prev_real_insn (insn);
  699.               rtx dest
  700.             = gen_lowpart (insn_operand_mode[insn_code_number][1],
  701.                        recog_operand[0]);
  702.               rtx newinsn
  703.             = emit_insn_before (gen_move_insn (dest,
  704.                                recog_operand[1]),
  705.                         insn);
  706.  
  707.               /* If this insn was the start of a basic block,
  708.              include the new insn in that block.
  709.              We need not check for code_label here;
  710.              while a basic block can start with a code_label,
  711.              INSN could not be at the beginning of that block.  */
  712.               if (previnsn == 0 || GET_CODE (previnsn) == JUMP_INSN)
  713.             {
  714.               int b;
  715.               for (b = 0; b < n_basic_blocks; b++)
  716.                 if (insn == basic_block_head[b])
  717.                   basic_block_head[b] = newinsn;
  718.             }
  719.  
  720.               /* This makes one more setting of new insns's dest. */
  721.               reg_n_sets[REGNO (recog_operand[0])]++;
  722.  
  723.               *recog_operand_loc[1] = recog_operand[0];
  724.               for (i = insn_n_dups[insn_code_number] - 1; i >= 0; i--)
  725.             if (recog_dup_num[i] == 1)
  726.               *recog_dup_loc[i] = recog_operand[0];
  727.  
  728.               insn = PREV_INSN (newinsn);
  729.               continue;
  730.             }
  731.  
  732.           /* If this is setting a pseudo from another pseudo or the
  733.              sum of a pseudo and a constant integer and the other
  734.              pseudo is known to be a pointer, set the destination to
  735.              be a pointer as well.
  736.  
  737.              Likewise if it is setting the destination from an address
  738.              or from a value equivalent to an address or to the sum of
  739.              an address and something else.
  740.              
  741.              But don't do any of this if the pseudo corresponds to
  742.              a user variable since it should have already been set
  743.              as a pointer based on the type.
  744.  
  745.              There is no point in doing this during our second
  746.              pass since not enough should have changed.  */
  747.  
  748.           if (pass == 0 && set != 0 && GET_CODE (SET_DEST (set)) == REG
  749.               && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
  750.               && ! REG_USERVAR_P (SET_DEST (set))
  751.               && ! REGNO_POINTER_FLAG (REGNO (SET_DEST (set)))
  752.               && ((GET_CODE (SET_SRC (set)) == REG
  753.                && REGNO_POINTER_FLAG (REGNO (SET_SRC (set))))
  754.               || ((GET_CODE (SET_SRC (set)) == PLUS
  755.                    || GET_CODE (SET_SRC (set)) == LO_SUM)
  756.                   && (GET_CODE (XEXP (SET_SRC (set), 1))
  757.                   == CONST_INT)
  758.                   && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
  759.                   && REGNO_POINTER_FLAG (REGNO (XEXP (SET_SRC (set), 0))))
  760.               || GET_CODE (SET_SRC (set)) == CONST
  761.               || GET_CODE (SET_SRC (set)) == SYMBOL_REF
  762.               || GET_CODE (SET_SRC (set)) == LABEL_REF
  763.               || (GET_CODE (SET_SRC (set)) == HIGH
  764.                   && (GET_CODE (XEXP (SET_SRC (set), 0)) == CONST
  765.                   || (GET_CODE (XEXP (SET_SRC (set), 0))
  766.                       == SYMBOL_REF)
  767.                   || (GET_CODE (XEXP (SET_SRC (set), 0))
  768.                       == LABEL_REF)))
  769.               || ((GET_CODE (SET_SRC (set)) == PLUS
  770.                    || GET_CODE (SET_SRC (set)) == LO_SUM)
  771.                   && (GET_CODE (XEXP (SET_SRC (set), 1)) == CONST
  772.                   || (GET_CODE (XEXP (SET_SRC (set), 1))
  773.                       == SYMBOL_REF)
  774.                   || (GET_CODE (XEXP (SET_SRC (set), 1))
  775.                       == LABEL_REF)))
  776.               || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
  777.                   && (GET_CODE (XEXP (note, 0)) == CONST
  778.                   || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
  779.                   || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
  780.             REGNO_POINTER_FLAG (REGNO (SET_DEST (set))) = 1;
  781.  
  782.           for (i = 0; i < noperands; i++)
  783.             {
  784.               constraints[i]
  785.             = insn_operand_constraint[insn_code_number][i];
  786.               modes[i] = insn_operand_mode[insn_code_number][i];
  787.             }
  788.         }
  789.  
  790.           /* If we get here, we are set up to record the costs of all the
  791.          operands for this insn.  Start by initializing the costs.
  792.          Then handle any address registers.  Finally record the desired
  793.          classes for any pseudos, doing it twice if some pair of
  794.          operands are commutative.  */
  795.          
  796.           for (i = 0; i < noperands; i++)
  797.         {
  798.           op_costs[i] = init_cost;
  799.  
  800.           if (GET_CODE (recog_operand[i]) == SUBREG)
  801.             recog_operand[i] = SUBREG_REG (recog_operand[i]);
  802.  
  803.           if (GET_CODE (recog_operand[i]) == MEM)
  804.             record_address_regs (XEXP (recog_operand[i], 0),
  805.                      BASE_REG_CLASS, loop_cost * 2);
  806.           else if (constraints[i][0] == 'p')
  807.             record_address_regs (recog_operand[i],
  808.                      BASE_REG_CLASS, loop_cost * 2);
  809.         }
  810.  
  811.           /* Check for commutative in a separate loop so everything will
  812.          have been initialized.  Don't bother doing anything if the
  813.          second operand is a constant since that is the case
  814.          for which the constraints should have been written.  */
  815.           
  816.           for (i = 0; i < noperands - 1; i++)
  817.         if (constraints[i][0] == '%'
  818.             && ! CONSTANT_P (recog_operand[i+1]))
  819.           {
  820.             char *xconstraints[MAX_RECOG_OPERANDS];
  821.             int j;
  822.  
  823.             /* Handle commutative operands by swapping the constraints.
  824.                We assume the modes are the same.  */
  825.  
  826.             for (j = 0; j < noperands; j++)
  827.               xconstraints[j] = constraints[j];
  828.  
  829.             xconstraints[i] = constraints[i+1];
  830.             xconstraints[i+1] = constraints[i];
  831.             record_reg_classes (nalternatives, noperands,
  832.                     recog_operand, modes, xconstraints,
  833.                     insn);
  834.           }
  835.  
  836.           record_reg_classes (nalternatives, noperands, recog_operand,
  837.                   modes, constraints, insn);
  838.  
  839.           /* Now add the cost for each operand to the total costs for
  840.          its register.  */
  841.  
  842.           for (i = 0; i < noperands; i++)
  843.         if (GET_CODE (recog_operand[i]) == REG
  844.             && REGNO (recog_operand[i]) >= FIRST_PSEUDO_REGISTER)
  845.           {
  846.             int regno = REGNO (recog_operand[i]);
  847.             struct costs *p = &costs[regno], *q = &op_costs[i];
  848.  
  849.             p->mem_cost += q->mem_cost * loop_cost;
  850.             for (j = 0; j < N_REG_CLASSES; j++)
  851.               p->cost[j] += q->cost[j] * loop_cost;
  852.           }
  853.         }
  854.     }
  855.  
  856.       /* Now for each register look at how desirable each class is
  857.      and find which class is preferred.  Store that in
  858.      `prefclass[REGNO]'.  Record in `altclass[REGNO]' the largest register
  859.      class any of whose registers is better than memory.  */
  860.     
  861.       if (pass == 0)
  862.     {
  863.       prefclass = (char *) oballoc (nregs);
  864.       altclass = (char *) oballoc (nregs);
  865.     }
  866.  
  867.       for (i = FIRST_PSEUDO_REGISTER; i < nregs; i++)
  868.     {
  869.       register int best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
  870.       enum reg_class best = ALL_REGS, alt = NO_REGS;
  871.       /* This is an enum reg_class, but we call it an int
  872.          to save lots of casts.  */
  873.       register int class;
  874.       register struct costs *p = &costs[i];
  875.  
  876.       for (class = (int) ALL_REGS - 1; class > 0; class--)
  877.         {
  878.           /* Ignore classes that are too small for this operand or
  879.          invalid for a operand that was auto-incremented.  */
  880.           if (CLASS_MAX_NREGS (class, PSEUDO_REGNO_MODE (i))
  881.           > reg_class_size[class]
  882. #ifdef FORBIDDEN_INC_DEC_CLASSES
  883.           || (in_inc_dec[i] && forbidden_inc_dec_class[class])
  884. #endif
  885.           )
  886.         ;
  887.           else if (p->cost[class] < best_cost)
  888.         {
  889.           best_cost = p->cost[class];
  890.           best = (enum reg_class) class;
  891.         }
  892.           else if (p->cost[class] == best_cost)
  893.         best = reg_class_subunion[(int)best][class];
  894.         }
  895.  
  896.       /* Record the alternate register class; i.e., a class for which
  897.          every register in it is better than using memory.  If adding a
  898.          class would make a smaller class (i.e., no union of just those
  899.          classes exists), skip that class.  The major unions of classes
  900.          should be provided as a register class.  Don't do this if we
  901.          will be doing it again later.  */
  902.  
  903.       if (pass == 1 || ! flag_expensive_optimizations)
  904.         for (class = 0; class < N_REG_CLASSES; class++)
  905.           if (p->cost[class] < p->mem_cost
  906.           && (reg_class_size[(int) reg_class_subunion[(int) alt][class]]
  907.               > reg_class_size[(int) alt])
  908. #ifdef FORBIDDEN_INC_DEC_CLASSES
  909.           && ! (in_inc_dec[i] && forbidden_inc_dec_class[class])
  910. #endif
  911.           )
  912.         alt = reg_class_subunion[(int) alt][class];
  913.       
  914.       /* If we don't add any classes, nothing to try.  */
  915.       if (alt == best)
  916.         alt = (int) NO_REGS;
  917.  
  918.       /* We cast to (int) because (char) hits bugs in some compilers.  */
  919.       prefclass[i] = (int) best;
  920.       altclass[i] = (int) alt;
  921.     }
  922.     }
  923. #endif /* REGISTER_CONSTRAINTS */
  924. }
  925.  
  926. #ifdef REGISTER_CONSTRAINTS
  927.  
  928. /* Record the cost of using memory or registers of various classes for
  929.    the operands in INSN.
  930.  
  931.    N_ALTS is the number of alternatives.
  932.  
  933.    N_OPS is the number of operands.
  934.  
  935.    OPS is an array of the operands.
  936.  
  937.    MODES are the modes of the operands, in case any are VOIDmode.
  938.  
  939.    CONSTRAINTS are the constraints to use for the operands.  This array
  940.    is modified by this procedure.
  941.  
  942.    This procedure works alternative by alternative.  For each alternative
  943.    we assume that we will be able to allocate all pseudos to their ideal
  944.    register class and calculate the cost of using that alternative.  Then
  945.    we compute for each operand that is a pseudo-register, the cost of 
  946.    having the pseudo allocated to each register class and using it in that
  947.    alternative.  To this cost is added the cost of the alternative.
  948.  
  949.    The cost of each class for this insn is its lowest cost among all the
  950.    alternatives.  */
  951.  
  952. static void
  953. record_reg_classes (n_alts, n_ops, ops, modes, constraints, insn)
  954.      int n_alts;
  955.      int n_ops;
  956.      rtx *ops;
  957.      enum machine_mode *modes;
  958.      char **constraints;
  959.      rtx insn;
  960. {
  961.   int alt;
  962.   enum op_type {OP_READ, OP_WRITE, OP_READ_WRITE} op_types[MAX_RECOG_OPERANDS];
  963.   int i, j;
  964.  
  965.   /* By default, each operand is an input operand.  */
  966.  
  967.   for (i = 0; i < n_ops; i++)
  968.     op_types[i] = OP_READ;
  969.  
  970.   /* Process each alternative, each time minimizing an operand's cost with
  971.      the cost for each operand in that alternative.  */
  972.  
  973.   for (alt = 0; alt < n_alts; alt++)
  974.     {
  975.       struct costs this_op_costs[MAX_RECOG_OPERANDS];
  976.       int alt_fail = 0;
  977.       int alt_cost = 0;
  978.       enum reg_class classes[MAX_RECOG_OPERANDS];
  979.       int class;
  980.  
  981.       for (i = 0; i < n_ops; i++)
  982.     {
  983.       char *p = constraints[i];
  984.       rtx op = ops[i];
  985.       enum machine_mode mode = modes[i];
  986.       int allows_mem = 0;
  987.       int win = 0;
  988.       char c;
  989.  
  990.       /* If this operand has no constraints at all, we can conclude 
  991.          nothing about it since anything is valid.  */
  992.  
  993.       if (*p == 0)
  994.         {
  995.           if (GET_CODE (op) == REG && REGNO (op) >= FIRST_PSEUDO_REGISTER)
  996.         bzero ((char *) &this_op_costs[i], sizeof this_op_costs[i]);
  997.  
  998.           continue;
  999.         }
  1000.  
  1001.       if (*p == '%')
  1002.         p++;
  1003.  
  1004.       /* If this alternative is only relevant when this operand
  1005.          matches a previous operand, we do different things depending
  1006.          on whether this operand is a pseudo-reg or not.  */
  1007.  
  1008.       if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
  1009.         {
  1010.           j = p[0] - '0';
  1011.           classes[i] = classes[j];
  1012.  
  1013.           if (GET_CODE (op) != REG || REGNO (op) < FIRST_PSEUDO_REGISTER)
  1014.         {
  1015.           /* If this matches the other operand, we have no added
  1016.              cost.  */
  1017.           if (rtx_equal_p (ops[j], op))
  1018.             ;
  1019.  
  1020.           /* If we can't put the other operand into a register, this
  1021.              alternative can't be used.  */
  1022.  
  1023.           else if (classes[j] == NO_REGS)
  1024.             alt_fail = 1;
  1025.  
  1026.           /* Otherwise, add to the cost of this alternative the cost
  1027.              to copy this operand to the register used for the other
  1028.              operand.  */
  1029.  
  1030.           else
  1031.             alt_cost += copy_cost (op, mode, classes[j], 1);
  1032.         }
  1033.           else if (GET_CODE (ops[j]) != REG
  1034.                || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
  1035.         {
  1036.           /* This op is a pseudo but the one it matches is not.  */
  1037.           
  1038.           /* If we can't put the other operand into a register, this
  1039.              alternative can't be used.  */
  1040.  
  1041.           if (classes[j] == NO_REGS)
  1042.             alt_fail = 1;
  1043.  
  1044.           /* Otherwise, add to the cost of this alternative the cost
  1045.              to copy the other operand to the register used for this
  1046.              operand.  */
  1047.  
  1048.           else
  1049.             alt_cost += copy_cost (ops[j], mode, classes[j], 1);
  1050.         }
  1051.           else
  1052.         {
  1053.           /* The costs of this operand are the same as that of the
  1054.              other operand.  However, if we cannot tie them, this
  1055.              alternative needs to do a copy, which is one
  1056.              instruction.  */
  1057.  
  1058.           this_op_costs[i] = this_op_costs[j];
  1059.           if (! find_reg_note (insn, REG_DEAD, op))
  1060.             alt_cost += 2;
  1061.  
  1062.           /* This is in place of ordinary cost computation
  1063.              for this operand.  */
  1064.           continue;
  1065.         }
  1066.         }
  1067.  
  1068.       /* Scan all the constraint letters.  See if the operand matches
  1069.          any of the constraints.  Collect the valid register classes
  1070.          and see if this operand accepts memory.  */
  1071.  
  1072.       classes[i] = NO_REGS;
  1073.       while (*p && (c = *p++) != ',')
  1074.         switch (c)
  1075.           {
  1076.           case '=':
  1077.         op_types[i] = OP_WRITE;
  1078.         break;
  1079.  
  1080.           case '+':
  1081.         op_types[i] = OP_READ_WRITE;
  1082.         break;
  1083.  
  1084.           case '*':
  1085.         /* Ignore the next letter for this pass.  */
  1086.         p++;
  1087.         break;
  1088.  
  1089.           case '%':
  1090.           case '?':  case '!':  case '#':
  1091.           case '&':
  1092.           case '0':  case '1':  case '2':  case '3':  case '4':
  1093.           case 'p':
  1094.         break;
  1095.  
  1096.           case 'm':  case 'o':  case 'V':
  1097.         /* It doesn't seem worth distingishing between offsettable
  1098.            and non-offsettable addresses here.  */
  1099.         allows_mem = 1;
  1100.         if (GET_CODE (op) == MEM)
  1101.           win = 1;
  1102.         break;
  1103.  
  1104.           case '<':
  1105.         if (GET_CODE (op) == MEM
  1106.             && (GET_CODE (XEXP (op, 0)) == PRE_DEC
  1107.             || GET_CODE (XEXP (op, 0)) == POST_DEC))
  1108.           win = 1;
  1109.         break;
  1110.  
  1111.           case '>':
  1112.         if (GET_CODE (op) == MEM
  1113.             && (GET_CODE (XEXP (op, 0)) == PRE_INC
  1114.             || GET_CODE (XEXP (op, 0)) == POST_INC))
  1115.           win = 1;
  1116.         break;
  1117.  
  1118.           case 'E':
  1119.         /* Match any floating double constant, but only if
  1120.            we can examine the bits of it reliably.  */
  1121.         if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT
  1122.              || HOST_BITS_PER_WIDE_INT != BITS_PER_WORD)
  1123.             && GET_MODE (op) != VOIDmode && ! flag_pretend_float)
  1124.           break;
  1125.         if (GET_CODE (op) == CONST_DOUBLE)
  1126.           win = 1;
  1127.         break;
  1128.  
  1129.           case 'F':
  1130.         if (GET_CODE (op) == CONST_DOUBLE)
  1131.           win = 1;
  1132.         break;
  1133.  
  1134.           case 'G':
  1135.           case 'H':
  1136.         if (GET_CODE (op) == CONST_DOUBLE
  1137.             && CONST_DOUBLE_OK_FOR_LETTER_P (op, c))
  1138.           win = 1;
  1139.         break;
  1140.  
  1141.           case 's':
  1142.         if (GET_CODE (op) == CONST_INT
  1143.             || (GET_CODE (op) == CONST_DOUBLE
  1144.             && GET_MODE (op) == VOIDmode))
  1145.           break;
  1146.           case 'i':
  1147.         if (CONSTANT_P (op)
  1148. #ifdef LEGITIMATE_PIC_OPERAND_P
  1149.             && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
  1150. #endif
  1151.             )
  1152.           win = 1;
  1153.         break;
  1154.  
  1155.           case 'n':
  1156.         if (GET_CODE (op) == CONST_INT
  1157.             || (GET_CODE (op) == CONST_DOUBLE
  1158.             && GET_MODE (op) == VOIDmode))
  1159.           win = 1;
  1160.         break;
  1161.  
  1162.           case 'I':
  1163.           case 'J':
  1164.           case 'K':
  1165.           case 'L':
  1166.           case 'M':
  1167.           case 'N':
  1168.           case 'O':
  1169.           case 'P':
  1170.         if (GET_CODE (op) == CONST_INT
  1171.             && CONST_OK_FOR_LETTER_P (INTVAL (op), c))
  1172.           win = 1;
  1173.         break;
  1174.  
  1175.           case 'X':
  1176.         win = 1;
  1177.         break;
  1178.  
  1179. #ifdef EXTRA_CONSTRAINT
  1180.               case 'Q':
  1181.               case 'R':
  1182.               case 'S':
  1183.               case 'T':
  1184.               case 'U':
  1185.         if (EXTRA_CONSTRAINT (op, c))
  1186.           win = 1;
  1187.         break;
  1188. #endif
  1189.  
  1190.           case 'g':
  1191.         if (GET_CODE (op) == MEM
  1192.             || (CONSTANT_P (op)
  1193. #ifdef LEGITIMATE_PIC_OPERAND_P
  1194.             && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
  1195. #endif
  1196.             ))
  1197.           win = 1;
  1198.         allows_mem = 1;
  1199.           case 'r':
  1200.         classes[i]
  1201.           = reg_class_subunion[(int) classes[i]][(int) GENERAL_REGS];
  1202.         break;
  1203.  
  1204.           default:
  1205.         classes[i]
  1206.           = reg_class_subunion[(int) classes[i]]
  1207.             [(int) REG_CLASS_FROM_LETTER (c)];
  1208.           }
  1209.  
  1210.       constraints[i] = p;
  1211.  
  1212.       /* How we account for this operand now depends on whether it is  a
  1213.          pseudo register or not.  If it is, we first check if any
  1214.          register classes are valid.  If not, we ignore this alternative,
  1215.          since we want to assume that all pseudos get allocated for
  1216.          register preferencing.  If some register class is valid, compute
  1217.          the costs of moving the pseudo into that class.  */
  1218.  
  1219.       if (GET_CODE (op) == REG && REGNO (op) >= FIRST_PSEUDO_REGISTER)
  1220.         {
  1221.           if (classes[i] == NO_REGS)
  1222.         alt_fail = 1;
  1223.           else
  1224.         {
  1225.           struct costs *pp = &this_op_costs[i];
  1226.  
  1227.           for (class = 0; class < N_REG_CLASSES; class++)
  1228.             pp->cost[class] = may_move_cost[class][(int) classes[i]];
  1229.  
  1230.           /* If the alternative actually allows memory, make things
  1231.              a bit cheaper since we won't need an extra insn to
  1232.              load it.  */
  1233.  
  1234.           pp->mem_cost = MEMORY_MOVE_COST (mode) - allows_mem;
  1235.  
  1236.           /* If we have assigned a class to this register in our
  1237.              first pass, add a cost to this alternative corresponding
  1238.              to what we would add if this register were not in the
  1239.              appropriate class.  */
  1240.  
  1241.           if (prefclass)
  1242.             alt_cost
  1243.               += may_move_cost[prefclass[REGNO (op)]][(int) classes[i]];
  1244.         }
  1245.         }
  1246.  
  1247.       /* Otherwise, if this alternative wins, either because we
  1248.          have already determined that or if we have a hard register of
  1249.          the proper class, there is no cost for this alternative.  */
  1250.  
  1251.       else if (win
  1252.            || (GET_CODE (op) == REG
  1253.                && reg_fits_class_p (op, classes[i], 0, GET_MODE (op))))
  1254.         ;
  1255.  
  1256.       /* If registers are valid, the cost of this alternative includes
  1257.          copying the object to and/or from a register.  */
  1258.  
  1259.       else if (classes[i] != NO_REGS)
  1260.         {
  1261.           if (op_types[i] != OP_WRITE)
  1262.         alt_cost += copy_cost (op, mode, classes[i], 1);
  1263.  
  1264.           if (op_types[i] != OP_READ)
  1265.         alt_cost += copy_cost (op, mode, classes[i], 0);
  1266.         }
  1267.  
  1268.       /* The only other way this alternative can be used is if this is a
  1269.          constant that could be placed into memory.  */
  1270.  
  1271.       else if (CONSTANT_P (op) && allows_mem)
  1272.         alt_cost += MEMORY_MOVE_COST (mode);
  1273.       else
  1274.         alt_fail = 1;
  1275.     }
  1276.  
  1277.       if (alt_fail)
  1278.     continue;
  1279.  
  1280.       /* Finally, update the costs with the information we've calculated
  1281.      about this alternative.  */
  1282.  
  1283.       for (i = 0; i < n_ops; i++)
  1284.     if (GET_CODE (ops[i]) == REG
  1285.         && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
  1286.       {
  1287.         struct costs *pp = &op_costs[i], *qq = &this_op_costs[i];
  1288.         int scale = 1 + (op_types[i] == OP_READ_WRITE);
  1289.  
  1290.         pp->mem_cost = MIN (pp->mem_cost,
  1291.                 (qq->mem_cost + alt_cost) * scale);
  1292.  
  1293.         for (class = 0; class < N_REG_CLASSES; class++)
  1294.           pp->cost[class] = MIN (pp->cost[class],
  1295.                      (qq->cost[class] + alt_cost) * scale);
  1296.       }
  1297.     }
  1298. }
  1299.  
  1300. /* Compute the cost of loading X into (if TO_P is non-zero) or from (if
  1301.    TO_P is zero) a register of class CLASS in mode MODE.
  1302.  
  1303.    X must not be a pseudo.  */
  1304.  
  1305. static int
  1306. copy_cost (x, mode, class, to_p)
  1307.      rtx x;
  1308.      enum machine_mode mode;
  1309.      enum reg_class class;
  1310.      int to_p;
  1311. {
  1312.   enum reg_class secondary_class = NO_REGS;
  1313.  
  1314.   /* If X is a SCRATCH, there is actually nothing to move since we are
  1315.      assuming optimal allocation.  */
  1316.  
  1317.   if (GET_CODE (x) == SCRATCH)
  1318.     return 0;
  1319.  
  1320.   /* Get the class we will actually use for a reload.  */
  1321.   class = PREFERRED_RELOAD_CLASS (x, class);
  1322.  
  1323. #ifdef HAVE_SECONDARY_RELOADS
  1324.   /* If we need a secondary reload (we assume here that we are using 
  1325.      the secondary reload as an intermediate, not a scratch register), the
  1326.      cost is that to load the input into the intermediate register, then
  1327.      to copy them.  We use a special value of TO_P to avoid recursion.  */
  1328.  
  1329. #ifdef SECONDARY_INPUT_RELOAD_CLASS
  1330.   if (to_p == 1)
  1331.     secondary_class = SECONDARY_INPUT_RELOAD_CLASS (class, mode, x);
  1332. #endif
  1333.  
  1334. #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
  1335.   if (! to_p)
  1336.     secondary_class = SECONDARY_OUTPUT_RELOAD_CLASS (class, mode, x);
  1337. #endif
  1338.  
  1339.   if (secondary_class != NO_REGS)
  1340.     return (move_cost[(int) secondary_class][(int) class]
  1341.         + copy_cost (x, mode, secondary_class, 2));
  1342. #endif  /* HAVE_SECONDARY_RELOADS */
  1343.  
  1344.   /* For memory, use the memory move cost, for (hard) registers, use the
  1345.      cost to move between the register classes, and use 2 for everything
  1346.      else (constants).  */
  1347.  
  1348.   if (GET_CODE (x) == MEM || class == NO_REGS)
  1349.     return MEMORY_MOVE_COST (mode);
  1350.  
  1351.   else if (GET_CODE (x) == REG)
  1352.     return move_cost[(int) REGNO_REG_CLASS (REGNO (x))][(int) class];
  1353.  
  1354.   else
  1355.     /* If this is a constant, we may eventually want to call rtx_cost here.  */
  1356.     return 2;
  1357. }
  1358.  
  1359. /* Record the pseudo registers we must reload into hard registers
  1360.    in a subexpression of a memory address, X.
  1361.  
  1362.    CLASS is the class that the register needs to be in and is either
  1363.    BASE_REG_CLASS or INDEX_REG_CLASS.
  1364.  
  1365.    SCALE is twice the amount to multiply the cost by (it is twice so we
  1366.    can represent half-cost adjustments).  */
  1367.  
  1368. static void
  1369. record_address_regs (x, class, scale)
  1370.      rtx x;
  1371.      enum reg_class class;
  1372.      int scale;
  1373. {
  1374.   register enum rtx_code code = GET_CODE (x);
  1375.  
  1376.   switch (code)
  1377.     {
  1378.     case CONST_INT:
  1379.     case CONST:
  1380.     case CC0:
  1381.     case PC:
  1382.     case SYMBOL_REF:
  1383.     case LABEL_REF:
  1384.       return;
  1385.  
  1386.     case PLUS:
  1387.       /* When we have an address that is a sum,
  1388.      we must determine whether registers are "base" or "index" regs.
  1389.      If there is a sum of two registers, we must choose one to be
  1390.      the "base".  Luckily, we can use the REGNO_POINTER_FLAG
  1391.      to make a good choice most of the time.  We only need to do this
  1392.      on machines that can have two registers in an address and where
  1393.      the base and index register classes are different.
  1394.  
  1395.      ??? This code used to set REGNO_POINTER_FLAG in some cases, but
  1396.      that seems bogus since it should only be set when we are sure
  1397.      the register is being used as a pointer.  */
  1398.  
  1399.       {
  1400.     rtx arg0 = XEXP (x, 0);
  1401.     rtx arg1 = XEXP (x, 1);
  1402.     register enum rtx_code code0 = GET_CODE (arg0);
  1403.     register enum rtx_code code1 = GET_CODE (arg1);
  1404.  
  1405.     /* Look inside subregs.  */
  1406.     if (code0 == SUBREG)
  1407.       arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
  1408.     if (code1 == SUBREG)
  1409.       arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
  1410.  
  1411.     /* If this machine only allows one register per address, it must
  1412.        be in the first operand.  */
  1413.  
  1414.     if (MAX_REGS_PER_ADDRESS == 1)
  1415.       record_address_regs (arg0, class, scale);
  1416.  
  1417.     /* If index and base registers are the same on this machine, just
  1418.        record registers in any non-constant operands.  We assume here,
  1419.        as well as in the tests below, that all addresses are in 
  1420.        canonical form.  */
  1421.  
  1422.     else if (INDEX_REG_CLASS == BASE_REG_CLASS)
  1423.       {
  1424.         record_address_regs (arg0, class, scale);
  1425.         if (! CONSTANT_P (arg1))
  1426.           record_address_regs (arg1, class, scale);
  1427.       }
  1428.  
  1429.     /* If the second operand is a constant integer, it doesn't change
  1430.        what class the first operand must be.  */
  1431.  
  1432.     else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
  1433.       record_address_regs (arg0, class, scale);
  1434.  
  1435.     /* If the second operand is a symbolic constant, the first operand
  1436.        must be an index register.  */
  1437.  
  1438.     else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
  1439.       record_address_regs (arg0, INDEX_REG_CLASS, scale);
  1440.  
  1441.     /* If this the sum of two registers where the first is known to be a 
  1442.        pointer, it must be a base register with the second an index.  */
  1443.  
  1444.     else if (code0 == REG && code1 == REG
  1445.          && REGNO_POINTER_FLAG (REGNO (arg0)))
  1446.       {
  1447.         record_address_regs (arg0, BASE_REG_CLASS, scale);
  1448.         record_address_regs (arg1, INDEX_REG_CLASS, scale);
  1449.       }
  1450.  
  1451.     /* If this is the sum of two registers and neither is known to
  1452.        be a pointer, count equal chances that each might be a base
  1453.        or index register.  This case should be rare.  */
  1454.  
  1455.     else if (code0 == REG && code1 == REG
  1456.          && ! REGNO_POINTER_FLAG (REGNO (arg0))
  1457.          && ! REGNO_POINTER_FLAG (REGNO (arg1)))
  1458.       {
  1459.         record_address_regs (arg0, BASE_REG_CLASS, scale / 2);
  1460.         record_address_regs (arg0, INDEX_REG_CLASS, scale / 2);
  1461.         record_address_regs (arg1, BASE_REG_CLASS, scale / 2);
  1462.         record_address_regs (arg1, INDEX_REG_CLASS, scale / 2);
  1463.       }
  1464.  
  1465.     /* In all other cases, the first operand is an index and the
  1466.        second is the base.  */
  1467.  
  1468.     else
  1469.       {
  1470.         record_address_regs (arg0, INDEX_REG_CLASS, scale);
  1471.         record_address_regs (arg1, BASE_REG_CLASS, scale);
  1472.       }
  1473.       }
  1474.       break;
  1475.  
  1476.     case POST_INC:
  1477.     case PRE_INC:
  1478.     case POST_DEC:
  1479.     case PRE_DEC:
  1480.       /* Double the importance of a pseudo register that is incremented
  1481.      or decremented, since it would take two extra insns
  1482.      if it ends up in the wrong place.  If the operand is a pseudo,
  1483.      show it is being used in an INC_DEC context.  */
  1484.  
  1485. #ifdef FORBIDDEN_INC_DEC_CLASSES
  1486.       if (GET_CODE (XEXP (x, 0)) == REG
  1487.       && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
  1488.     in_inc_dec[REGNO (XEXP (x, 0))] = 1;
  1489. #endif
  1490.  
  1491.       record_address_regs (XEXP (x, 0), class, 2 * scale);
  1492.       break;
  1493.  
  1494.     case REG:
  1495.       {
  1496.     register struct costs *pp = &costs[REGNO (x)];
  1497.     register int i;
  1498.  
  1499.     pp->mem_cost += (MEMORY_MOVE_COST (DPmode) * scale) / 2;
  1500.  
  1501.     for (i = 0; i < N_REG_CLASSES; i++)
  1502.       pp->cost[i] += (may_move_cost[i][(int) class] * scale) / 2;
  1503.       }
  1504.       break;
  1505.  
  1506.     default:
  1507.       {
  1508.     register char *fmt = GET_RTX_FORMAT (code);
  1509.     register int i;
  1510.     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1511.       if (fmt[i] == 'e')
  1512.         record_address_regs (XEXP (x, i), class, scale);
  1513.       }
  1514.     }
  1515. }
  1516. #endif /* REGISTER_CONSTRAINTS */
  1517.  
  1518. /* This is the `regscan' pass of the compiler, run just before cse
  1519.    and again just before loop.
  1520.  
  1521.    It finds the first and last use of each pseudo-register
  1522.    and records them in the vectors regno_first_uid, regno_last_uid
  1523.    and counts the number of sets in the vector reg_n_sets.
  1524.  
  1525.    REPEAT is nonzero the second time this is called.  */
  1526.  
  1527. /* Indexed by pseudo register number, gives uid of first insn using the reg
  1528.    (as of the time reg_scan is called).  */
  1529.  
  1530. int *regno_first_uid;
  1531.  
  1532. /* Indexed by pseudo register number, gives uid of last insn using the reg
  1533.    (as of the time reg_scan is called).  */
  1534.  
  1535. int *regno_last_uid;
  1536.  
  1537. /* Record the number of registers we used when we allocated the above two
  1538.    tables.  If we are called again with more than this, we must re-allocate
  1539.    the tables.  */
  1540.  
  1541. static int highest_regno_in_uid_map;
  1542.  
  1543. /* Maximum number of parallel sets and clobbers in any insn in this fn.
  1544.    Always at least 3, since the combiner could put that many togetherm
  1545.    and we want this to remain correct for all the remaining passes.  */
  1546.  
  1547. int max_parallel;
  1548.  
  1549. void reg_scan_mark_refs ();
  1550.  
  1551. void
  1552. reg_scan (f, nregs, repeat)
  1553.      rtx f;
  1554.      int nregs;
  1555.      int repeat;
  1556. {
  1557.   register rtx insn;
  1558.  
  1559.   if (!repeat || nregs > highest_regno_in_uid_map)
  1560.     {
  1561.       /* Leave some spare space in case more regs are allocated.  */
  1562.       highest_regno_in_uid_map = nregs + nregs / 20;
  1563.       regno_first_uid
  1564.     = (int *) oballoc (highest_regno_in_uid_map * sizeof (int));
  1565.       regno_last_uid
  1566.     = (int *) oballoc (highest_regno_in_uid_map * sizeof (int));
  1567.       reg_n_sets
  1568.     = (short *) oballoc (highest_regno_in_uid_map * sizeof (short));
  1569.     }
  1570.  
  1571.   bzero (regno_first_uid, highest_regno_in_uid_map * sizeof (int));
  1572.   bzero (regno_last_uid, highest_regno_in_uid_map * sizeof (int));
  1573.   bzero (reg_n_sets, highest_regno_in_uid_map * sizeof (short));
  1574.  
  1575.   max_parallel = 3;
  1576.  
  1577.   for (insn = f; insn; insn = NEXT_INSN (insn))
  1578.     if (GET_CODE (insn) == INSN
  1579.     || GET_CODE (insn) == CALL_INSN
  1580.     || GET_CODE (insn) == JUMP_INSN)
  1581.       {
  1582.     if (GET_CODE (PATTERN (insn)) == PARALLEL
  1583.         && XVECLEN (PATTERN (insn), 0) > max_parallel)
  1584.       max_parallel = XVECLEN (PATTERN (insn), 0);
  1585.     reg_scan_mark_refs (PATTERN (insn), INSN_UID (insn));
  1586.       }
  1587. }
  1588.  
  1589. void
  1590. reg_scan_mark_refs (x, uid)
  1591.      rtx x;
  1592.      int uid;
  1593. {
  1594.   register enum rtx_code code = GET_CODE (x);
  1595.   register rtx dest;
  1596.  
  1597.   switch (code)
  1598.     {
  1599.     case CONST_INT:
  1600.     case CONST:
  1601.     case CONST_DOUBLE:
  1602.     case CC0:
  1603.     case PC:
  1604.     case SYMBOL_REF:
  1605.     case LABEL_REF:
  1606.     case ADDR_VEC:
  1607.     case ADDR_DIFF_VEC:
  1608.       return;
  1609.  
  1610.     case REG:
  1611.       {
  1612.     register int regno = REGNO (x);
  1613.  
  1614.     regno_last_uid[regno] = uid;
  1615.     if (regno_first_uid[regno] == 0)
  1616.       regno_first_uid[regno] = uid;
  1617.       }
  1618.       break;
  1619.  
  1620.     case SET:
  1621.       /* Count a set of the destination if it is a register.  */
  1622.       for (dest = SET_DEST (x);
  1623.        GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
  1624.        || GET_CODE (dest) == ZERO_EXTEND;
  1625.        dest = XEXP (dest, 0))
  1626.     ;
  1627.  
  1628.       if (GET_CODE (dest) == REG)
  1629.     reg_n_sets[REGNO (dest)]++;
  1630.  
  1631.       /* ... fall through ... */
  1632.  
  1633.     default:
  1634.       {
  1635.     register char *fmt = GET_RTX_FORMAT (code);
  1636.     register int i;
  1637.     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1638.       {
  1639.         if (fmt[i] == 'e')
  1640.           reg_scan_mark_refs (XEXP (x, i), uid);
  1641.         else if (fmt[i] == 'E' && XVEC (x, i) != 0)
  1642.           {
  1643.         register int j;
  1644.         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  1645.           reg_scan_mark_refs (XVECEXP (x, i, j), uid);          
  1646.           }
  1647.       }
  1648.       }
  1649.     }
  1650. }
  1651.  
  1652. /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
  1653.    is also in C2.  */
  1654.  
  1655. int
  1656. reg_class_subset_p (c1, c2)
  1657.      register enum reg_class c1;
  1658.      register enum reg_class c2;
  1659. {
  1660.   if (c1 == c2) return 1;
  1661.  
  1662.   if (c2 == ALL_REGS)
  1663.   win:
  1664.     return 1;
  1665.   GO_IF_HARD_REG_SUBSET (reg_class_contents[(int)c1],
  1666.              reg_class_contents[(int)c2],
  1667.              win);
  1668.   return 0;
  1669. }
  1670.  
  1671. /* Return nonzero if there is a register that is in both C1 and C2.  */
  1672.  
  1673. int
  1674. reg_classes_intersect_p (c1, c2)
  1675.      register enum reg_class c1;
  1676.      register enum reg_class c2;
  1677. {
  1678. #ifdef HARD_REG_SET
  1679.   register
  1680. #endif
  1681.     HARD_REG_SET c;
  1682.  
  1683.   if (c1 == c2) return 1;
  1684.  
  1685.   if (c1 == ALL_REGS || c2 == ALL_REGS)
  1686.     return 1;
  1687.  
  1688.   COPY_HARD_REG_SET (c, reg_class_contents[(int) c1]);
  1689.   AND_HARD_REG_SET (c, reg_class_contents[(int) c2]);
  1690.  
  1691.   GO_IF_HARD_REG_SUBSET (c, reg_class_contents[(int) NO_REGS], lose);
  1692.   return 1;
  1693.  
  1694.  lose:
  1695.   return 0;
  1696. }
  1697.  
  1698.